package org.xc.uvc_camera;

import android.text.TextUtils;

import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.FFmpegExecuteResponseHandler;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

class Mp4Util {

    private Mp4Util() {}
    private static Mp4Util instance;
    synchronized static Mp4Util getInstance() {
        if (instance == null) {
            instance = new Mp4Util();
        }
        return instance;
    }
    boolean isUploading = false;

    void start(String fileName) {
        if (!isUploading && !TextUtils.isEmpty(fileName)) {
            new UploadThread(fileName).start();
        }
    }

    private class UploadThread extends Thread {

        private String fileName;
        UploadThread(String fileName) {
            this.fileName = fileName;
        }

        @Override
        public void run() {
            super.run();
            isUploading = true;

            List<File> mp4FileList = FileUtil.getMp4Files(fileName,7);
            contact(fileName,mp4FileList,7);

            try {
                //线程休眠后八时间
                Thread.sleep( Constant.BA);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            mp4FileList = FileUtil.getMp4Files(fileName,8);
            contact(fileName,mp4FileList,8);

            isUploading = false;
        }
    }

    private void contact(String fileName,List<File> mp4FileList,int type) {
        if (mp4FileList == null || mp4FileList.isEmpty()) {
            return;
        }
        try {
            File fileMp4Txt = FileUtil.getFile(fileName + "_" + type + ".txt");
            FileOutputStream fos = new FileOutputStream(fileMp4Txt);
            for(File file: mp4FileList) {
                String s = "file '" + file.getAbsolutePath()+ "'";
                fos.write(s.getBytes());
                fos.write(System.getProperty("line.separator").getBytes());
            }
            fos.flush();
            fos.close();

            File mp4 = FileUtil.getFile(fileName + "_" + type  +  "_c.mp4");
            String cmd = "-f concat -safe 0 -i " + fileMp4Txt.getAbsolutePath() + " -c copy " + mp4.getAbsolutePath();
            FFmpeg.getInstance(MyApplication.INSTANCE).execute(cmd.split(" "), new FFmpegExecuteResponseHandler() {
                @Override
                public void onSuccess(String message) {
                    UploadUtil.Companion.uploadFile(mp4.getAbsolutePath());
                }

                @Override
                public void onProgress(String message) {

                }

                @Override
                public void onFailure(String message) {
                    writeError(fileName,message,type);
                }

                @Override
                public void onStart() {

                }

                @Override
                public void onFinish() {

                }
            });
        } catch (Exception e) {
            writeError(fileName,e.getMessage(),type);
        }
    }

    private void writeError(String error,String fileName,int type) {
        if (TextUtils.isEmpty(error) || TextUtils.isEmpty(fileName)) {
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(FileUtil.createFile(fileName + "_" + type  + "_error" + ".txt"));
            fos.write(error.getBytes());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
